package org.example.springmessage.controller;

import org.example.springmessage.domain.User;
import org.example.springmessage.refult.R;
import org.example.springmessage.service.UserService;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/getAllUsers")
    public Object getAllUsers()
    {
        return userService.getAllUsers();
    }

    @RequestMapping("/getUserById")
    public R getUserById(Integer id)
    {
        return R.ok(userService.getUserById(id));
    }

    @PostMapping("/updateUser")
    public R updateUser(User user, @AuthenticationPrincipal UserDetails userDetails,String old_password)
    {
        user.setId(Integer.valueOf(userDetails.getAuthorities().toString().substring(1,2)));
        User oldUser = userService.getUserById(user.getId());
        user.setPhoto_url(oldUser.getPhoto_url());
        if (oldUser.getPassword().equals(old_password)){
            userService.update(user);
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("/register")
    public R register(User user) {
        if (userService.register(user)){
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("/photo")
    public R upload(@RequestParam("photo") MultipartFile file, @AuthenticationPrincipal UserDetails user){
        try {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 创建文件路径
            Path filePath = Paths.get("src/main/resources/static/photo/"+ user.getAuthorities().toString().substring(1,2)+".jpg");
            // 确保目标文件夹存在
            Files.createDirectories(filePath.getParent());

            // 将文件保存到目标路径
            Files.write(filePath, file.getBytes());

            Map<String, String> response = new HashMap<>();
            response.put("message", "文件上传成功: " + fileName);
            response.put("filePath", filePath.toString());
            User user1 = new User();
            user1.setPhoto_url(filePath.toString());
            user1.setId(Integer.valueOf(user.getAuthorities().toString().substring(1,2)));
            userService.updatePhoto(user1);
            return R.ok();

        } catch (Exception e) {
            e.printStackTrace();
            Map<String, String> response = new HashMap<>();
            response.put("message", "文件上传失败: " + e.getMessage());
            return R.error();
        }
    }


    @GetMapping("/getPhoto")
    public R getImage(@AuthenticationPrincipal UserDetails user) {
        User userById = userService.getUserById(Integer.valueOf(user.getAuthorities().toString().substring(1, 2)));
        String[] path = userById.getPhoto_url().split("\\\\");
        System.out.println(path[path.length-1]);
        return R.ok(path[path.length-1]);
    }
}

